<style>
.modal-body{max-height:none;}
.editprice-input{width:100px;}
</style>
<!-- 模态框（Modal） -->
<div id="edit-price" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="width: 650px;display: none;overflow: overlay;">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="margin-right: 10px;">×</button>
		<h3 id="H1">修改价格</h3>
	</div>
	<div class="modal-body">
		<table class="table table-striped table-main table-order-header">
			<colgroup>
				<col style="width: 40%;">
				<col style="width: 20%;">
				<col style="width: 25%;">
				<col style="width: 15%;">
			</colgroup>
			<tbody>
				<tr>
					<th>商品信息</th>
					<th>商品清单</th>
					<th>
						<div class="editprice-tiptxt">涨价或减价<i class="icon-tip"></i>
							<p class="text-tip">-表示减价<i class="icon-down-pic"></i></p>
						</div>
					</th>
					<th>邮费</th>
				</tr>
			</tbody>
		</table>
		<table class="table table-bordered table-order-list">
			<colgroup>
				<col style="width: 40%;">
				<col style="width: 20%;">
				<col style="width: 25%;">
				<col style="width: 15%;">
			</colgroup>
			<tbody id="OrderCommodity"></tbody>
		</table>
		<ul class="edit-price-amountpay">
			<li>
				<span class="amountpay-label">商品总价：</span>
				<span class="amountpay-price" id="goodsmoney"></span>元&nbsp;&nbsp;&nbsp;
				<span class="amountpay-label">商品优惠：</span>
				<span class="amountpay-price" id="discountmoney"></span>元&nbsp;&nbsp;&nbsp;
				<span class="amountpay-label">运费：</span>
				<span class="amountpay-price" id="modifiedFreight"></span>元
			</li>
			<li>
				<div>
					实收款： <span class="amountpay-price reality-price" id="changeTotal"></span>元
					<input type="hidden" id="hiedchangeTotal" />
				</div>
			</li>
		</ul>
	</div>
	<div class="modal-footer">
		<button class="btn btn-info" onclick="updPrice()" id="butSubmit" data-dismiss="modal" aria-hidden="true" style="display:inline-block;">保存</button>
		<button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>
	</div>
</div>

<div class="modal fade" id="Delivery" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display:none;">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title" id="myModalLabel">商品发货</h4>
			</div>
			<div class="modal-body">
				<!-- 主要内容 -->
				<div>待发货<span id="no_shipping_num"></span>，已选<span id="checkedbox">0</span></div>
				<table class="table table-hover" style="margin-bottom:10px;">
					<thead>
						<tr>
							<th><label class="checkbox-inline"><input type="checkbox" id="inlineCheckbox1" value="option1" onclick="deliveryCheckAll(this)"></label></th>
							<th>商品</th>
							<th>数量</th>
							<th>物流 | 单号</th>
							<th>状态</th>
						</tr>
					</thead>
					<colgroup>
						<col style="width: 5%;">
						<col style="width: 40%;">
						<col style="width: 10%;">
						<col style="width: 30%;">
						<col style="width: 15%;">
					<colgroup>
					<tbody></tbody>
				</table>
				<div>
					发货方式：<label class="checkbox-inline"><input type="radio" name=shipping_type id="shipping_type1" value="1" checked="checked"> 需要物流</label>
						<label class="checkbox-inline"><input type="radio" name="shipping_type" id="shipping_type0"  value="0"> 无需物流</label>
				</div>
				<div class="form-group" id="express_input">
					<select class="form-control input-lg" id="divlogistics_express_company" style="margin-bottom:5px;"></select>
					<div class="col-lg-2"><input type="text" id="divlogistics_express_no" class="form-control" placeholder="请填写快递单号"></div>
				</div>
				<div id="receiver_info"></div>
			</div>
			
			<div class="modal-footer">
				<input type="hidden" id="delivery_order_id" value=""/>
				<button type="button" class="btn btn-info" onclick="orderDeliverySubmit()" style="display:inline-block;">提交更改</button>
				<button type="button" class="btn" data-dismiss="modal">关闭</button>
			</div>
		</div>
	</div>
</div>

<script>
$("#shipping_type1").focus(function(){
	$("#express_input").show();
});

$("#shipping_type0").focus(function(){
	$("#express_input").hide();
});
/*****订单相关操作函数开始*****/
function operation(operation_type, order_id){
	if(operation_type == 'pay'){
		orderOffLinePay(order_id);//线下支付
	}else if(operation_type == 'complate'){
		orderComplete(order_id);//交易完成
	}else if(operation_type == 'delivery'){
		orderDelivery(order_id);//发货
	}else if(operation_type == 'close'){
		orderClose(order_id);//交易关闭
	}else if(operation_type == 'adjust_price'){
		modifyPrice(order_id);//修改价格
	}
}

function orderDelivery(order_id){
	$("#Delivery").modal('show');
	$("#divlogistics_express_company option").remove();
	$("#Delivery .modal-body table tbody tr").remove();
	$.ajax({
		type : "post",
		url : "ADMIN_MAIN/Order/orderDeliveryData",
		data : {'order_id':order_id},
		success : function(data) {
			$("#delivery_order_id").val(order_id);
			var receiver_info = '收货信息：'+data['order_info']['address']+'&nbsp;'+data['order_info']['receiver_address']+'&nbsp;'+data['order_info']['receiver_name']+'&nbsp;'+data['order_info']['receiver_mobile'];//收货信息
			$("#receiver_info").html(receiver_info);
			var co_html = '';
			co_html += '<option value="0">请选择物流公司</option>';
			for(var i=0;i<data['express_company_list'].length;i++){
				if(data['express_company_list'][i]['is_enabled'] == '1'){
					co_html += '<option value="'+data["express_company_list"][i]["co_id"]+'">'+data["express_company_list"][i]["company_name"]+'</option>';
				}
			}
			$("#divlogistics_express_company").append(co_html);
			var go_html = '';
			var no_shipping_num = 0;
			for(var i=0;i<data['order_goods_list'].length;i++){
				if(data['order_goods_list'][i]['shipping_status'] == 0){
					no_shipping_num++;
				}
				go_html += '<tr>';
				if(data['order_goods_list'][i]['shipping_status'] > 0){
					go_html += '<td><label class="checkbox-inline"><input type="checkbox" value="'+data['order_goods_list'][i]['shipping_status']+'" disabled="true"></label></td>';
				}else{
					go_html += '<td><label class="checkbox-inline"><input type="checkbox" id="'+data['order_goods_list'][i]['order_goods_id']+'" value="'+data['order_goods_list'][i]['shipping_status']+'" onclick="deliveryCheck(this)"></label></td>';
				}
				go_html += '<td><a>'+data['order_goods_list'][i]['goods_name']+'</a></td>';
				go_html += '<td>'+data['order_goods_list'][i]['num']+'</td>';
				if(data['order_goods_list'][i]['shipping_status'] == 0 || data['order_goods_list'][i]['express_info']['express_company'] == undefined){
					go_html += '<td></td>';
				}else{
					go_html += '<td>'+data['order_goods_list'][i]['express_info']['express_company']+' | '+data['order_goods_list'][i]['express_info']['express_no']+'</td>';
				}
				go_html += '<td>'+data['order_goods_list'][i]['shipping_status_name']+'</td>';
				go_html += '</tr>';
			}
			$("#no_shipping_num").html(no_shipping_num);
			$("#Delivery .modal-body table tbody").append(go_html);
		}
	});
}

function orderDeliverySubmit(){
	var order_id = $("#delivery_order_id").val();
	var order_goods_id_array = '';
	$("#Delivery .modal-body table tbody input[type = 'checkbox'][value = 0][checked]").each(function(i){
		if(0==i){
			order_goods_id_array = $(this).attr('id');
		}else{
			order_goods_id_array += (","+$(this).attr('id'));
		}
	});
	if(order_goods_id_array == ''){
		showMessage('error', '必须选择商品');
		return false;		
	}
	var express_name = $("#divlogistics_express_company").find("option:selected").text();
	var shipping_type = $('#Delivery input[name="shipping_type"]:checked ').val();
	var express_company_id = $("#divlogistics_express_company").val();
	var express_no = $("#divlogistics_express_no").val();
	$.ajax({
		type : "post",
		url : "ADMIN_MAIN/Order/orderDelivery",
		data : {'order_id':order_id,"order_goods_id_array":order_goods_id_array,"express_name":express_name,"shipping_type":shipping_type,"express_company_id":express_company_id,"express_no":express_no},
		success : function(data) {
			$("#Delivery").modal('hide');
			if (data['code'] > 0) {
				showMessage('success', data["message"],window.location.reload());
			} else {
				showMessage('error', data["message"]);
			}
		}
	});
}

function deliveryCheckAll(event){
	var checked = event.checked;
	$("#Delivery .modal-body table tbody input[type = 'checkbox'][value = 0]").prop("checked",checked);
	var obj = $("#Delivery .modal-body table tbody input[type = 'checkbox'][value = 0][checked]");
	$("#checkedbox").html(obj.length);
}

function deliveryCheck(event){
	var obj = $("#Delivery .modal-body table tbody input[type = 'checkbox'][value = 0][checked]");
	$("#checkedbox").html(obj.length);
}

//全选
function CheckAll(event){
	var checked = event.checked;
	$(".style0list tbody input[type = 'checkbox']").prop("checked",checked);
}

function orderOffLinePay(order_id){
	$.ajax({
		type : "post",
		url : "ADMIN_MAIN/Order/orderOffLinePay",
		data : {'order_id':order_id},
		success : function(data) {
			if (data["code"] > 0) {
				showMessage('success', data["message"],window.location.reload());
			}else{
				showMessage('error', data["message"]);
			}
		}
	});
}
function orderComplete(order_id){
	$.ajax({
		type : "post",
		url : "ADMIN_MAIN/Order/orderComplete",
		data : {'order_id':order_id},
		success : function(data) {
			if (data["code"] > 0) {
				showMessage('success', data["message"],window.location.reload());
			}else{
				showMessage('error', data["message"]);
			}
		}
	});
}

function orderClose(order_id){
	$( "#dialog" ).dialog({
		buttons: {"确定,#e57373": function() {
			$(this).dialog('close');
			$.ajax({
				type : "post",
				url : "ADMIN_MAIN/Order/orderClose",
				data : {"order_id" : order_id},
				success : function(data) {
					if(data["code"] > 0 ){
						LoadingInfo(1);
						showMessage('success', data["message"],window.location.reload());
					}
				}
			})
		}
	},
	contentText:"你确定关闭该订单吗？",
	title:"消息提醒"
	});
}

//修改价格
function modifyPrice(order_id,orderFreight){
	if(orderFreight == null){
		orderFreight = 0;
	}
	orderInfo ={
    	express_fee: 0,
		total: 0,
		goodsArray: new Array()
	};
	$("#butSubmit").val(order_id);
		var str = "";
		var Total = 0.00;
		var Count = 0;
		$.ajax({
			type: "post",
			url: "ADMIN_MAIN/Order/getOrderGoods",
			data: { "order_id": order_id },
			dataType: "json",
			async: false,
			success: function (jsonData) {
				var Subtotal = 0.0;
				var order_info = jsonData[1];
				jsonData = jsonData[0];
				for (var i = 0 ; i < jsonData.length; i++) {
					Price = (jsonData[i].price * 1);
					Count = (jsonData[i].num * 1);
					Subtotal = parseFloat(Price) * parseInt(Count);//单商品总价
					Total += parseFloat(Subtotal * 1);
					str += "<tr>";
					str += "  <td>";
					str += "<div class='product-img'><img src='__ROOT__/"+jsonData[i]['picture_info']['pic_cover_micro'] + "'></div>";
					str += "<div class='product-infor'>";
					//原总金额
					var item_now_money = parseFloat(jsonData[i].price*jsonData[i].num)+parseFloat(jsonData[i].adjust_money);
					str += "<input type='hidden' id='total"+jsonData[i].order_goods_id+"' value='"+item_now_money*(item_now_money/parseFloat(jsonData[i].goods_money))+"'>";
					str += "<a class='name' href='APP_MAIN?id="+jsonData[i].goods_id+"' target='_blank'>" + jsonData[i].goods_name + "</a>";
					str += "<p class='specification'><span>规格:" + jsonData[i].sku_name + "</span></p>";
					str += "<div class='div-flag-style'>";
					str += "</div>";
					str += "</div>";
					str += "</td>";
					str += "<td>";
					str += "<div class='cell'><span name='Commoditymark' count='" + jsonData[i].num + "' id='" + jsonData[i].goods_id + "' dir='" + jsonData[i].price + "' value='" + jsonData[i].price + "'>￥" + jsonData[i].price + "</span></div>";
					str += "<div class='cell' id='Count" + jsonData[i].goods_id + "' value='" + jsonData[i].num + "'>" + jsonData[i].num + "件</div>";
					/*if(jsonData[i].adjust_money != 0){
						str += "<div class='cell' style='color:gray;'>(" + jsonData[i].adjust_money + "元)</div>";
					} */
					str += "</td>";
					str += "<td>";
					str += "<div class='editprice-discount'><input  type='hidden' id='productPrice" + jsonData[i].order_goods_id + "' value='" + jsonData[i].price + "'><input type='hidden' id='count" + jsonData[i].goods_id + "' value='" + jsonData[i].num + "'>";
					str += "<div class='editprice-minus'><input name='caculatePrice'  onchange=\"caculatePrice()\" id='" + jsonData[i].order_goods_id + "' value='"+jsonData[i].adjust_money+"'  class='editprice-input' type='number'  placeholder='0'  /></div>";
					str += "</td>"; 
					if (i == 0) {
						str += "<td rowspan='"+jsonData.length+"'>";
						str += "<input onchange=\"caculatePrice()\" id='Freightnumber'type='number' placeholder='0'  class='editprice-input' value='"+order_info.shipping_money+"' ";
						if(orderFreight != 0 || orderFreight != ''){
							str += orderFreight;
						}
						str += "' min='0'/>";
						str += "<p class='muted'>直接输入邮费金额</p>";
						str += "<input type='hidden' id='hidorderNumber' value='" + jsonData[i].order_id + "'>";
						str += "<input type='hidden' id='freighthidden' value='" + orderFreight + "'>";
						str += "<input type='hidden' id='goodsmoneyhidden' value=''>";
						str += "<input type='hidden' id='favourable' value='0'>";
						str += "</td>";
						str += "</tr>";
					}
					$("#OrderCommodity").html(str);
					$("#changeTotal").html(Total.toFixed(2));
					$("#goodsmoney").html(order_info.goods_money);
					$("#goodsmoneyhidden").val(Total);
					var discount_money =order_info.point_money*1.00+order_info.coupon_money*1.00+order_info.user_money*1.00+order_info.promotion_money*1.00;
					$("#discountmoney").html(discount_money);
					$("#changeTotal").html(order_info.pay_money); 
					$("#hiedchangeTotal").html(Total);
				}
				$("#modifiedFreight").html(order_info.shipping_money);
				var freight = $("#Freightnumber").val() == 0 ? 0 : $("#Freightnumber").val(); 
				$('#edit-price').modal('show');
			}
		});
	}
	
	//重新计算
	function caculatePrice(){
		//设置邮费
		var Freightnumber = $("#Freightnumber").val();//邮费input
		$("#modifiedFreight").html(Freightnumber);
		//调整金额
		var price = 0.00;
		$("input[name = 'caculatePrice']").each(function(i){
			if(0 == i){
				price = parseFloat($(this).val());
			}else{
				price = parseFloat($(this).val()) + parseFloat(price);
			}
		});
		var goods_money  = $("#goodsmoneyhidden").val();
		new_goods_money = parseFloat(price)+parseFloat(goods_money);
		$("#goodsmoney").html(new_goods_money);
		// 获取邮费
		var modifiedFreight = $("#modifiedFreight").html(); 
		// 获取优惠金额
		var discountmoney = $("#discountmoney").html();
		//计算实收款
		new_hiedchangeTotal = parseFloat(new_goods_money)+parseFloat(modifiedFreight)-parseFloat(discountmoney);
		$("#changeTotal").html(new_hiedchangeTotal);
	}
	
	/**
	* 保存修改的价格
	* $order_id, $goods_money, $shipping_fee
	*/
	function updPrice(){
		var order_id = $("#hidorderNumber").val();
		var order_goods_id_adjust_array = '';
		var shipping_fee = $("#Freightnumber").val();
		$("input[name = 'caculatePrice']").each(function(i){
			if(0 == i){
				order_goods_id_adjust_array = $(this).attr('id')+','+$(this).val();
			}else{
				order_goods_id_adjust_array += ';'+$(this).attr('id')+','+$(this).val();
			}
		});
		$.ajax({
			type: "post",
			url: "ADMIN_MAIN/Order/orderAdjustMoney",
			data: { "order_id": order_id, "order_goods_id_adjust_array":order_goods_id_adjust_array, "shipping_fee":shipping_fee},
			dataType: "json",
			async: false,
			success: function (data) {
			if (data["code"] > 0) {
					showMessage('success', data["message"],window.location.reload());
				}else{
					showMessage('error', data["message"]);
				}
			}
		});
	}
/*********订单相关操作函数结束*********/
</script>